﻿using System;
using System.Collections.Generic;
using System.Web.Http;
using LeHouse.Server.WebApi.Common;
using System.IO;
using LeHouse.Server.Dto;
using LeHouse.Server.Service;

namespace LeHouse.Server.WebApi.Controllers
{
    public class ExaminationController : ApiController
    {
        /// <summary>
        /// 输出初审核报告书
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Examination/Thetrial")]
        public dynamic Thetrial([FromBody]string P_ReportNumber)
        {
           var obj=transmitHelper.transmitHelperByReportNumber(P_ReportNumber);
           jsonManager jm = new jsonManager();

            if(obj==null)
            {
                jm.code = 0;
                jm.msg = "该项目号下没有任何数据";
                return Json(jm);
            }
          
            if(!(obj.T_State>4))
            {
                jm.code = 0;
                jm.msg = "未通过初审，不能导出";
                return Json(jm);
            }

            string TemplateName = AppDomain.CurrentDomain.BaseDirectory
                + "Word\\Template\\抵押物初审意见书模板.doc";
            if (!File.Exists(TemplateName))
            {
                return "errer";
            }
            string SaveName = AppDomain.CurrentDomain.BaseDirectory
                + string.Format("Word\\Temp\\抵押物初审意见书{0}.doc", obj.T_ReportNumber);

            List<WordsTemp> lwt = new List<WordsTemp>();
            lwt.Add(new WordsTemp() { Name = "报告号", Value = obj.T_ReportNumber });
            lwt.Add(new WordsTemp() { Name = "姓名", Value = obj.T_Linkman != null ? obj.T_Linkman : "[数据为空]" });
            lwt.Add(new WordsTemp() { Name = "身份证号", Value = obj.T_Idcard != null ? obj.T_Idcard : "[数据为空]" });
            lwt.Add(new WordsTemp() { Name = "坐落", Value = obj.T_Located != null ? obj.T_Located : "[数据为空]" });
            lwt.Add(new WordsTemp() { Name = "建筑面积", Value = "180" });
            lwt.Add(new WordsTemp() { Name = "价值时点", Value = "2014年12月11日" });
            lwt.Add(new WordsTemp() { Name = "总值", Value = "2010" });
            lwt.Add(new WordsTemp() { Name = "总值大写", Value = transmitHelper.chang(Convert.ToDouble("2010")) });
            lwt.Add(new WordsTemp() { Name = "抵押额", Value = "300" });
            lwt.Add(new WordsTemp() { Name = "抵押额大写", Value = "人民币叁佰万元整" });
            lwt.Add(new WordsTemp() { Name = "29", Value = obj.T_Loanyear != null ? obj.T_Loanyear : "[数据为空]" });
            lwt.Add(new WordsTemp() { Name = "买卖人姓名", Value = "李晓龙" });
            lwt.Add(new WordsTemp() { Name = "签约时间", Value = "2016年11月18日" });
            lwt.Add(new WordsTemp() { Name = "买卖合同编号", Value = "CW356274" });
            lwt.Add(new WordsTemp() { Name = "时间年月日", Value = DateTime.Now.ToString("yyyy年MM月dd日") });

            CommonWords.Word(TemplateName, SaveName, lwt);
            jm.code=1;
            jm.msg= string.Format("\\Word\\Temp\\抵押物初审意见书{0}.doc", obj.T_ReportNumber);
            return Json(jm);
        }


        /// <summary>
        /// 输出终审报告书
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Examination/lastAudit")]
        public dynamic lastAudit([FromBody]string T_ReportNumber)
        {
            var obj = transmitHelper.transmitHelperByReportNumber(T_ReportNumber);
            jsonManager jm = new jsonManager();

            if (obj == null)
            {
                jm.code = 0;
                jm.msg = "该项目号下没有任何数据";
                return Json(jm);
            }

            if (!(obj.T_State > 4))
            {
                jm.code = 0;
                jm.msg = "未通过终审，不能导出";
                return Json(jm);
            }
            //if( obj.P_reportID==1)//--todo根据类型选择模板
            //{

            //};
            string TemplateName = AppDomain.CurrentDomain.BaseDirectory
                + "Word\\Template\\模式1 报告模板2016-G01-118-00005号-李珊正式评估报告.doc";
            if (!File.Exists(TemplateName))
            {
                return "errer";
            }
            string SaveName = AppDomain.CurrentDomain.BaseDirectory
                + string.Format("Word\\Temp\\模式1 报告模板{0}号-李珊正式评估报告{1}.doc", "李三", obj.T_ReportNumber);

            List<WordsTemp> lwt = new List<WordsTemp>();
            lwt.Add(new WordsTemp() { Name = "报告号", Value = obj.T_ReportNumber });
            lwt.Add(new WordsTemp() { Name = "借款人", Value = "李三" });
            lwt.Add(new WordsTemp() { Name = "借款人身份证号", Value = "230223199007080211" });
            lwt.Add(new WordsTemp() { Name = "借款人联系电话", Value = "11011011110" });
            lwt.Add(new WordsTemp() { Name = "抵押房屋地址", Value = "延庆县康庄家园三区3号楼3层七单元706" });
            lwt.Add(new WordsTemp() { Name = "建筑面积", Value = "170" });
            lwt.Add(new WordsTemp() { Name = "评估总值", Value = "800" });
            lwt.Add(new WordsTemp() { Name = "最高抵押贷款额", Value = "160" });
            lwt.Add(new WordsTemp() { Name = "房屋剩余使用年限", Value = "50" });
            lwt.Add(new WordsTemp() { Name = "买卖人姓名", Value = "李晓龙" });
            lwt.Add(new WordsTemp() { Name = "签约时间", Value = "2016年11月18日" });
            lwt.Add(new WordsTemp() { Name = "买卖合同编号", Value = "CW356274" });
            lwt.Add(new WordsTemp() { Name = "时间年月日", Value = DateTime.Now.ToString("yyyy年MM月dd日") });

            CommonWords.Word(TemplateName, SaveName, lwt);
            jm.code = 1;
            jm.msg= string.Format("Word\\Temp\\模式1 报告模板{0}号-李珊正式评估报告{1}.doc", "李三", obj.T_ReportNumber);
            return Json(jm);
        }
       


    }
}
